package com.zhizhi.common.base;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * code is far away from bug with the animal protecting
 *
 * @Description :
 * ---------------------------------
 */
public abstract class BaseServiceImpl<M extends BaseDao<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {

    public static final Log logger = LogFactory.getLog(BaseServiceImpl.class);

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public M baseDao;

    @Override
    public Page<T> selectPage(Page<T> page, Wrapper<T> queryWrapper) {
        return (Page<T>) baseDao.selectPage(page, queryWrapper);
    }

    @Override
    public Page<Map<String, Object>> selectMapsPage(Page<T> page, Wrapper<T> queryWrapper) {
        return (Page<Map<String, Object>>) baseDao.selectMapsPage(page, queryWrapper);
    }

    @Override
    public Integer deleteById(Integer id) {
        return baseDao.deleteById(id);
    }

    @Override
    public T selectById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<T> getByIds(List<Integer> ids) {
        return baseDao.selectBatchIds(ids);
    }


    @Override
    public Map<Integer, T> encapsulationMap() {
        Map<Integer, T> result = new HashMap<>();
        List<T> list = list();
        list.forEach(i -> {
            result.put(i.getId(), i);
        });
        return result;
    }

    @Override
    public <O extends BaseEntity<O>> Map<Integer, O> listToMap(List<O> list){
        HashMap<Integer, O> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(t -> {
                map.put(t.getId(),t);
            });
        }
        return map;
    }

    @Override
    public boolean addValidator(String column, String value) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.eq(column, value);
        List<T> list = list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return false;
        }
        //校验通过
        return true;
    }


    @Override
    public boolean updateValidator(String column, String value, Integer id) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.eq(column, value);
        List<T> list = list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            if (list.size() == 1) {
                if (!Objects.equals(id, list.get(0).getId())) {
                    return false;
                }
            } else if (list.size() > 1) {
                return false;
            }
        }
        //校验通过
        return true;
    }

    @Override
    public Map<Integer, T> mapByIds(List<Integer> ids) {
        Map<Integer, T> result = new HashMap<>();
        if (CollectionUtil.isEmpty(ids)) {
            return result;
        }

        List<T> list = (List<T>) listByIds(ids);
        list.forEach(i -> {
            result.put(i.getId(), i);
        });
        return result;
    }


    @Override
    public List<Integer> getIds(List<T> list) {
        List<Integer> ids = new ArrayList<>();
        if (CollectionUtil.isEmpty(list)) {
            return ids;
        }
        list.forEach(i -> {
            ids.add(i.getId());
        });
        return ids;
    }


}